package sky.ikaros.cameraDemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.util.Size;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import java.io.ByteArrayOutputStream;

import sky.ikaros.camera.CameraRenderType;
import sky.ikaros.camera.SimpleCameraManager;

public class ActivityMain extends Activity {
    private static final String TAG = "ActivityMain";
    private SimpleCameraManager manager;
    private CameraRenderType type = CameraRenderType.BACKGROUND;
    private ImageView img;
    private RelativeLayout container;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = findViewById(R.id.container);
        img = findViewById(R.id.camera_img);
        Button open = findViewById(R.id.open);
        open.setOnClickListener(v -> {
            manager.open(0);
            manager.startPreview();
        });
        Button close = findViewById(R.id.close);
        close.setOnClickListener(v -> {
            manager.stopPreview();
            manager.close();
        });
        Button start = findViewById(R.id.start);
        start.setOnClickListener(v -> {
            manager.startPreview();
        });
        Button stop = findViewById(R.id.stop);
        stop.setOnClickListener(v -> {
            manager.stopPreview();
        });
        Button background = findViewById(R.id.background);
        background.setOnClickListener(v -> {
            manager.stopPreview();
            manager.close();
            type = CameraRenderType.BACKGROUND;
            manager.open(0);
            manager.startPreview();
        });
        Button foreign = findViewById(R.id.foreign);
        foreign.setOnClickListener(v -> {
            manager.stopPreview();
            manager.close();
            type = CameraRenderType.FOREGROUND;
            manager.open(0);
            manager.startPreview();
        });
        init();
    }

    private void init() {
        if (manager != null) return;
        SurfaceView sv = findViewById(R.id.camera_preview);
        manager = new SimpleCameraManager() {
            @Override
            public CameraRenderType getRenderType() {
                return type;
            }

            @Override
            public SurfaceView getForegroundRenderer() {
                return sv;
            }

            private Bitmap bitmap;

            @Override
            public void onPreviewFrame(byte[] data) {
//                Log.d(TAG, "onPreviewFrame: " + Thread.currentThread().getName());
                byte[] jpegData = transformNV21Data2ImageData(data);
                bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
                if (bitmap == null) return;
                runOnUiThread(() -> img.setImageBitmap(bitmap));
            }

            @Override
            protected Size getRequestSize() {
                return new Size(container.getWidth(), container.getHeight());
            }
        };
        manager.open(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        manager.startPreview();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.activity_entry, R.anim.activity_out);
    }

    @Override
    protected void onPause() {
        super.onPause();
        manager.stopPreview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.close();
        manager.release();
    }
}
